'use strict';

/**
 * Created by twer on 15/3/5.
 */
angular.module('photo')
    .controller('AlbumListCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'PageConstant',
        'PathSuffixConstant',
        'AlbumListService',
        function ($scope, $rootScope, $routeParams, PageConstant, PathSuffixConstant, AlbumListService) {
            $rootScope.visitedUserId = $routeParams.userId;
            var author = $routeParams.userId;

            $scope.query = function (pageNumber) {
                var queryCondition = {
                    offset: (pageNumber - 1) * PageConstant.countPerPage,
                    countPerPage: PageConstant.countPerPage,
                    ownerId: author + PathSuffixConstant
                };

                AlbumListService.query(queryCondition, function (response) {
                    results(response);
                });
            };

            $scope.remove = function (album) {
                if (!confirm('是否删除' + album.name) + '？') {
                    return;
                }

                AlbumListService.remove({albumId: album._id}, function () {
                    alert('删除成功！');
                    var newAlbums = [];
                    angular.forEach($scope.albums, function (a) {
                        if (a._id != album._id) {
                            newAlbums.push(a);
                        }
                    });
                    $scope.albums = newAlbums;
                    $scope.totalCount = $scope.totalCount - 1;
                });
            };

            function results(response) {
                $scope.albums = response.results;
                $scope.totalCount = response.totalCount;
            }

            $scope.query(1);

            $scope.$on('AlbumCreatedEventFromRoot', function (event, data) {
                $scope.categories = $scope.categories || [{
                        _id: data._id,
                        name: data.name,
                        createTime: new Date(),
                        photos: 0
                    }];
            })
        }])

    .controller('AlbumManagementCtrl', [
        '$scope',
        '$rootScope',
        '$cookies',
        'AlbumModalService',
        'AlbumService',
        function ($scope, $rootScope, $cookies, AlbumModalService, AlbumService) {
            $scope.close = AlbumModalService.deactivate;

            $scope.albumManagement = {
                show: AlbumModalService.activate
            };

            $scope.newAlbum = {
                name: '',
                description: '',
                author: {identity: $cookies.userid}
            };

            $scope.addAlbum = function () {
                if ($scope.newAlbum.name == '') {
                    alert('请输入新相册名称');
                    return;
                }

                AlbumService.save($scope.newAlbum, function (response) {
                    $scope.$emit('AlbumCreatedEvent', {
                        _id: response._id,
                        name: $scope.newAlbum.name
                    });
                    $scope.close();
                    $scope.newAlbum.name = '';
                });
            };

            $rootScope.$on('AlbumCreatedEvent', function (event, data) {
                $rootScope.$broadcast('AlbumCreatedEventFromRoot', data);
            });
        }]);


